package com.atguigu.tingshu.album.tasks;

import com.atguigu.tingshu.album.manager.VodManager;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dyl
 * @version 1.0
 * @description:
 * @date 2025/11/5 12:35
 */
@Component
public class SchedulerTasks {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodManager vodManager;

    @Scheduled(cron = "0 * * * * ?")
    public void checkVideoReviewTask() {
        List<TrackInfo> trackInfoReviewTaskIds = trackInfoService.list(new LambdaQueryWrapper<TrackInfo>()
                .eq(TrackInfo::getStatus, SystemConstant.TRACK_STATUS_IN_PASS)
                .isNotNull(TrackInfo::getReviewTaskId)
                .select(TrackInfo::getId, TrackInfo::getReviewTaskId)
                .last("LIMIT 100"));
        // 收集需要更新的TrackInfo对象
        List<TrackInfo> updateList = new ArrayList<>();
        for (TrackInfo trackInfoTask : trackInfoReviewTaskIds) {
            List<Boolean> taskResult = vodManager.getTaskResult(trackInfoTask.getReviewTaskId());
            // 任务未完成
            if (!taskResult.get(0)) {
                continue;
            }
            // 设置审核结果状态
            trackInfoTask.setStatus(taskResult.get(1) ? SystemConstant.TRACK_STATUS_PASS : SystemConstant.TRACK_STATUS_NO_PASS);
            updateList.add(trackInfoTask);
        }
        // 批量更新数据库
        if (!updateList.isEmpty()) {
            trackInfoService.updateBatchById(updateList);
        }
    }
}
